﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ESN.Models;
using ESN.Languages;
using System.Configuration;

namespace ESN.Controllers
{
    [Authorize]
    public class CanvasController : Controller
    {
        private ESNCoreDbContext core = new ESNCoreDbContext();

        public ActionResult Index()
        {
            ViewBag.UserId = Membership.GetUser().ProviderUserKey.ToString();
            return View();
        }
        public ActionResult Room(string id)
        {
            ViewBag.GroupId = id;
            Guid sid = new Guid(id);
            var session = core.CanvasSessions.Find(sid);
            if (session == null)
            {
                ViewBag.Detail = ESN.Languages.LangString.Load("not_exist", "Error");
                return View("Error");
            }
            if (session.UserId == (Guid)Membership.GetUser().ProviderUserKey)
            {
                ViewBag.readOnly = false;
                ViewBag.allowChat = true;
            }
            else
            {
                ViewBag.readOnly = session.readOnly;
                ViewBag.allowChat = session.allowChat;
            }
            ViewBag.ShareContent = LangString.Load("share", "Canvas") + session.Title + "(" + ConfigurationManager.AppSettings["ClientUrl"] + "/Canvas/Room/" + id.ToString() + " )";
            return View(session);
        }
        [HttpPost]
        public ActionResult NewCanvas(CanvasSession s)
        {
            if (ModelState.IsValid)
            {
                core.CanvasSessions.Add(s);
                core.SaveChanges();
            }
            return RedirectToAction("Room", "Canvas", new { id = s.ID });
        }
    }
}
